<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回分配给 PHP 的内存量</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.memory-get-usage" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">memory_get_usage</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.2, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">memory_get_usage</span> &mdash; <span class="dc-title">返回分配给 PHP 的内存量</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.memory-get-usage-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>memory_get_usage</strong></span>
    ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$real_usage</code><span class="initializer"> = false</span></span>
  ] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   返回当前分配给你的 PHP 脚本的内存量，单位是字节（byte）。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.memory-get-usage-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">real_usage</code></dt>

     <dd>

      <p class="para">
       如果设置为 <strong><code>TRUE</code></strong>，获取系统分配总的内存尺寸，包括未使用的页。如果未设置或者设置为 <strong><code>FALSE</code></strong>，仅仅报告实际使用的内存量。
      </p>
     </dd>

    
   </dl>

  </p>
  
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">PHP 不跟踪非<em>emalloc()</em> 分配的内存</p>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.memory-get-usage-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回内存量字节数。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.memory-get-usage-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.2.1</td>
       <td>
        不需要在编译时使用 <a href="ini.core.html#ini.memory-limit" class="link">--enable-memory-limit</a> 选项就能够使用这个函数。
       </td>
      </tr>

      <tr>
       <td>5.2.0</td>
       <td>
        增加了参数 <code class="parameter">real_usage</code>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.memory-get-usage-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-517">
    <p><strong>Example #1 一个 <span class="function"><strong>memory_get_usage()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//这只是个例子，下面的数字取决于你的系统<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">memory_get_usage</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;36640<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_repeat</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hello"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4242</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">memory_get_usage</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;57960<br /><br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">memory_get_usage</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;36744<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.memory-get-usage-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="memory_get_peak_usage.html" class="function" rel="rdfs-seeAlso">memory_get_peak_usage()</a> - 返回分配给 PHP 内存的峰值</span></li>
    <li class="member"><a href="ini.core.html#ini.memory-limit" class="link">memory_limit</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>